package com.totoro.motan.demo.config;

import com.totoro.motan.demo.properties.BasicRefererConfigProperties;
import com.totoro.motan.demo.properties.BasicServiceConfigProperties;
import com.totoro.motan.demo.properties.ProtocolConfigProperties;
import com.totoro.motan.demo.properties.RegistryConfigProperties;
import com.weibo.api.motan.config.ExtConfig;
import com.weibo.api.motan.config.springsupport.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * Created by Niki on 2018/5/23 17:25
 */
@Configuration
public class MotanConfig {

    /**
     * 注册中心配置bean名称
     */
    private static final String REGISTRY_CONFIG_BEAN_NAME = "_spring-boot-starter-motan-registry_";
    /**
     * 协议配置bean名称
     */
    private static final String PROTOCOL_CONFIG_BEAN_NAME = "_spring-boot-starter-motan-protocol_";

    /**
     * 配置Motan扫描的路径
     *
     * @param scanPackage
     * @return
     */
    @Bean
    public AnnotationBean annotationBean(@Value("${motan.annotation.package}") String scanPackage) {
        AnnotationBean annotationBean = new AnnotationBean();
        if (!StringUtils.isEmpty(scanPackage)) {
            annotationBean.setPackage(scanPackage);
        }
        return annotationBean;
    }

    @Bean(name = PROTOCOL_CONFIG_BEAN_NAME)
    public ProtocolConfigBean protocolConfigBean(ProtocolConfigProperties properties) {
        ProtocolConfigBean config = new ProtocolConfigBean();
        config.setName("motan"); //默认的配置名

        if (!StringUtils.isEmpty(properties.getName())) {
            config.setName(properties.getName());
        }

        if (!StringUtils.isEmpty(properties.getSerialization())) {
            config.setSerialization(properties.getSerialization());
        }

        if (properties.getIothreads() != null) {
            config.setIothreads(properties.getIothreads());
        }

        if (properties.getPoolLifo() != null) {
            config.setPoolLifo(properties.getPoolLifo());
        }

        if (properties.getLazyInit() != null) {
            config.setLazyInit(properties.getLazyInit());
        }

        if (!StringUtils.isEmpty(config.getEndpointFactory())) {
            config.setEndpointFactory(config.getEndpointFactory());
        }
        if (!StringUtils.isEmpty(config.getCluster())) {
            config.setCluster(config.getCluster());
        }

        if (!StringUtils.isEmpty(properties.getRequestTimeout())) {
            config.setRequestTimeout(properties.getRequestTimeout());
        }

        if (properties.getMinWorkerThread() != null) {
            config.setMinWorkerThread(properties.getMinWorkerThread());
        }
        if (properties.getMaxWorkerThread() != null) {
            config.setMaxWorkerThread(properties.getMaxWorkerThread());
        }
        if (properties.getMinClientConnection() != null) {
            config.setMinClientConnection(properties.getMinClientConnection());
        }
        if (properties.getMaxWorkerThread() != null) {
            config.setMaxClientConnection(properties.getMaxClientConnection());
        }
        if (properties.getMaxServerConnection() != null) {
            config.setMaxServerConnection(properties.getMaxServerConnection());
        }

        if (!StringUtils.isEmpty(properties.getLoadbalance())) {
            config.setLoadbalance(properties.getLoadbalance());
        }
        if (!StringUtils.isEmpty(properties.getHaStrategy())) {
            config.setHaStrategy(properties.getHaStrategy());
        }

        if (config.getWorkerQueueSize() != null) {
            config.setWorkerQueueSize(config.getWorkerQueueSize());
        }
        if (config.getAcceptConnections() != null) {
            config.setAcceptConnections(config.getAcceptConnections());
        }
        if (!StringUtils.isEmpty(config.getProxy())) {
            config.setProxy(config.getProxy());
        }
        if (!StringUtils.isEmpty(config.getFilter())) {
            config.setFilter(config.getFilter());
        }
        if (config.getRetries() != null) {
            config.setRetries(config.getRetries());
        }
        if (config.getAsync() != null) {
            config.setAsync(config.getAsync());
        }

        if (properties.getDefaultConfig() != null) {
            config.setDefault(properties.getDefaultConfig());
        }
        return config;
    }

    /**
     * 注册配置
     *
     * @return
     */
    @Bean(name = REGISTRY_CONFIG_BEAN_NAME)
    public RegistryConfigBean getRegistryConfigBean(RegistryConfigProperties properties) {
        RegistryConfigBean config = new RegistryConfigBean();
        config.setName(REGISTRY_CONFIG_BEAN_NAME);
        if (!StringUtils.isEmpty(properties.getRegProtocol())) {
            config.setRegProtocol(properties.getRegProtocol());
        }

        if (!StringUtils.isEmpty(properties.getAddress())) {
            config.setAddress(properties.getAddress());
        }

        if (!StringUtils.isEmpty(properties.getPort())) {
            config.setPort(properties.getPort());
        }

        if (!StringUtils.isEmpty(properties.getConnectTimeout())) {
            config.setConnectTimeout(properties.getConnectTimeout());
        }
        if (!StringUtils.isEmpty(properties.getRequestTimeout())) {
            config.setRequestTimeout(properties.getRequestTimeout());
        }
        if (properties.getRegistrySessionTimeout() != null) {
            config.setRegistrySessionTimeout(properties.getRegistrySessionTimeout());
        }

        if (properties.getRegister() != null) {
            config.setRegister(properties.getRegister());
        }
        if (properties.getRegistryRetryPeriod() != null) {
            config.setRegistryRetryPeriod(properties.getRegistryRetryPeriod());
        }
        if (!StringUtils.isEmpty(properties.getCheck())) {
            config.setCheck(properties.getCheck());
        }

        if (properties.getSubscribe() != null) {
            config.setSubscribe(properties.getSubscribe());
        }

        return config;
    }

    /**
     * 引用配置
     *
     * @param properties
     * @return
     */
    @Bean
    public BasicServiceConfigBean basicServiceConfigBean(BasicServiceConfigProperties properties, RegistryConfigBean registryConfigBean) {
        BasicServiceConfigBean config = new BasicServiceConfigBean();

        if (!StringUtils.isEmpty(properties.getExport())) {
            config.setExport(properties.getExport());
        } else {
            if (StringUtils.isEmpty(properties.getExportPort())) {
                throw new RuntimeException("need service export port...");
            }
            config.setExport(PROTOCOL_CONFIG_BEAN_NAME+":"+properties.getExportPort());
        }

        if (!StringUtils.isEmpty(properties.getExtConfigId())) {
            ExtConfig extConfig = new ExtConfig();
            extConfig.setId(properties.getExtConfigId());
            config.setExtConfig(extConfig);
        }
        if (!StringUtils.isEmpty(properties.getProxy())) {
            config.setProxy(properties.getProxy());
        }
        if (!StringUtils.isEmpty(properties.getGroup())) {
            config.setGroup(properties.getGroup());
        }
        if (!StringUtils.isEmpty(properties.getVersion())) {
            config.setVersion(properties.getVersion());
        }
        if (!StringUtils.isEmpty(properties.getThrowException())) {
            config.setThrowException(properties.getThrowException());
        }
        if (!StringUtils.isEmpty(properties.getApplication())) {
            config.setApplication(properties.getApplication());
        }
        if (!StringUtils.isEmpty(properties.getShareChannel())) {
            config.setShareChannel(properties.getShareChannel());
        }
        if (!StringUtils.isEmpty(properties.getRegistry())) {
            // 追加内部的注册配置bean
            config.setRegistry(REGISTRY_CONFIG_BEAN_NAME + "," + properties.getRegistry());
        } else {
            config.setRegistry(REGISTRY_CONFIG_BEAN_NAME);
        }
        if (!StringUtils.isEmpty(properties.getAccessLog())) {
            config.setAccessLog(properties.getAccessLog());
        }
        if (!StringUtils.isEmpty(properties.getUsegz())) {
            config.setUsegz(properties.getUsegz());
        }
        if (!StringUtils.isEmpty(properties.getMingzSize())) {
            config.setMingzSize(properties.getMingzSize());
        }
        if (!StringUtils.isEmpty(properties.getCodec())) {
            config.setCodec(properties.getCodec());
        }
        if (!StringUtils.isEmpty(properties.getFilter())) {
            config.setFilter(properties.getFilter());
        }
        if (!StringUtils.isEmpty(properties.getModule())) {
            config.setModule(properties.getModule());
        }
        if (properties.getActives() != null) {
            config.setActives(properties.getActives());
        }
        if (properties.getRegister() != null) {
            config.setRegister(properties.getRegister());
        }

        return config;
    }

    @Bean
    public BasicRefererConfigBean basicRefererConfigBean(BasicRefererConfigProperties properties) {
        BasicRefererConfigBean config = new BasicRefererConfigBean();
        config.setProtocol(PROTOCOL_CONFIG_BEAN_NAME);
        if (!StringUtils.isEmpty(properties.getGroup())) {
            config.setGroup(properties.getGroup());
        }
        if (!StringUtils.isEmpty(properties.getModule())) {
            config.setModule(properties.getModule());
        }
        if (!StringUtils.isEmpty(properties.getApplication())) {
            config.setApplication(properties.getApplication());
        }
        if (!StringUtils.isEmpty(properties.getRegistry())) {
            // 追加内部的注册配置bean
            config.setRegistry(REGISTRY_CONFIG_BEAN_NAME + "," + properties.getRegistry());
        } else {
            config.setRegistry(REGISTRY_CONFIG_BEAN_NAME);
        }
        if (!StringUtils.isEmpty(properties.getCheck())) {
            config.setCheck(properties.getCheck());
        }
        if (properties.getAccessLog() != null) {
            config.setAccessLog(properties.getAccessLog());
        }
        if (properties.getRetries() != null) {
            config.setRetries(properties.getRetries());
        }
        if (properties.getThrowException() != null) {
            config.setThrowException(properties.getThrowException());
        }
        if (!StringUtils.isEmpty(properties.getId())) {
            config.setId(properties.getId());
        }
        if (!StringUtils.isEmpty(properties.getVersion())) {
            config.setVersion(properties.getVersion());
        }
        if (properties.getShareChannel() != null) {
            config.setShareChannel(properties.getShareChannel());
        }
        if (properties.getRequestTimeout() != null) {
            config.setRequestTimeout(properties.getRequestTimeout());
        }
        if (!StringUtils.isEmpty(properties.getFilter())) {
            config.setFilter(properties.getFilter());
        }
        if (!StringUtils.isEmpty(properties.getExtConfigId())) {
            ExtConfig extConfig = new ExtConfig();
            extConfig.setId(properties.getExtConfigId());
            config.setExtConfig(extConfig);
        }
        if (properties.getActives() != null) {
            config.setActives(properties.getActives());
        }
        if (properties.getAsync() != null) {
            config.setAsync(properties.getAsync());
        }
        if (!StringUtils.isEmpty(properties.getCodec())) {
            config.setCodec(properties.getCodec());
        }
        if (properties.getUsegz() != null) {
            config.setUsegz(properties.getUsegz());
        }
        if (properties.getMingzSize() != null) {
            config.setMingzSize(properties.getMingzSize());
        }
        if (!StringUtils.isEmpty(properties.getProxy())) {
            config.setProxy(properties.getProxy());
        }
        if (!StringUtils.isEmpty(properties.getMock())) {
            config.setMock(properties.getMock());
        }

        // 文档未描述的4个属性暂不处理
        return config;
    }
}
